package com.atguigu.senior.dao.impl;

import com.atguigu.senior.dao.BaseDAO;
import com.atguigu.senior.dao.EmployeeDao;
import com.atguigu.senior.pojo.Employee;
import com.atguigu.senior.util.JDBCUtilV2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl extends BaseDAO implements EmployeeDao {
    @Override
    public List<Employee> selectAll() {
        String sql = "SELECT emp_id, emp_name, emp_salary, emp_age FROM t_emp";
        List<Employee> employeeList;
        try {
            employeeList = super.selectAll(Employee.class, sql, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return employeeList;
    }

    @Override
    public Employee selectByEmpId(Integer empId) {
        String sql = "SELECT emp_id, emp_name, emp_salary, emp_age FROM t_emp WHERE emp_id = ?";
        Employee employee;
        try {
            employee = super.selectQueryBean(Employee.class, sql, empId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return employee;
    }

    @Override
    public int insert(Employee employee) {
        String sql = "INSERT INTO t_emp(emp_name, emp_salary, emp_age) VALUES(?, ?, ?)";
        int row;
        try {
            row = super.executeUpdate(sql, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row;
    }

    @Override
    public int update(Employee employee) {
        String sql = "UPDATE t_emp SET emp_name = ?, emp_salary = ?, emp_age = ? WHERE emp_id = ?";
        int row;
        try {
            row = super.executeUpdate(sql, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge(), employee.getEmpId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row;
    }

    @Override
    public int delete(Integer empId) {
        String sql = "DELETE FROM t_emp WHERE emp_id = ?";
        int row;
        try {
            row = super.executeUpdate(sql, empId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row;
    }

    @Override
    public int deleteBatch(Integer[] empIds) {
        String sql = "DELETE FROM t_emp WHERE emp_id in ?";
        int row;
        try {
            row = super.executeUpdate(sql, empIds);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row;
    }
}
